<?php
//$_['action']={in|out|write|send|save|read|review|reply}
//write:
//	$_['rec_id']
//	$_['draft_id']
//reply:
//	$_['rep_id'] 回复哪条消息
//read/review:
//	$_['id'] 消息id
//	$_['raw']=1 是否直接显示html
//in/out:
//	$_['top'] 显示的短消息id<top
//send:
//	$_['rec_id']
//	$_['draft_id']
//	$_['title']
//	$_['content']

	include 'include/function.php';
	need_login();
	
	$message_action=get_request('action','in');

	if($message_action=='send') {
		$message_rec	= get_to_int(get_request('rec_id'));
		$message_draft	= get_to_int(get_request('draft_id')); //TODO
		$message_title	= str_to_mysql(get_request('title'));
		$message_content= str_to_mysql(get_request('content'));
		if($message_draft==0)
			oj_query('insert into message (sender_id,receiver_id,send_time,title,content,state) values ' .
				"($_user_id,$message_rec,now(),'$message_title','$message_content','sent') ");
		redirect('message.php?action=out');
	} else if($message_action=='save') {
		//TODO
	}
	
	show_header(_d('title_message'));
	echo '<center><a href="message.php?action=in">Inbox</a>&nbsp;&nbsp;<a href="message.php?action=out">Outbox</a></center>';
	if($message_action=='write'||$message_action=='reply') {
		if($message_action=='reply') { //回复
			$message_rep=get_to_int(get_request('rep_id')); //reply id
			if($message_rep) {
				$row=mysql_fetch_row(oj_query('select sender_id,title,content,send_time from message ' .
					"where id=$message_rep and receiver_id=$_user_id and state!='draft' limit 1"));
				if(!isset($row[0]))redirect_error('message not found');
				$message_rec	= $row[0];
				$message_rec_name=user_id_to_name($message_rec);
				$message_title	= 'Re: ' . $row[1];
				$message_content= "\n\n\n---Quote from $message_rec_name ({$row[3]}) ---\n" . $row[2] . 
					"\n---End of quote from $message_rec_name ({$row[3]}) ---\n";
			} else redirect_error('bad request');
		} else { //写新消息
			$message_rec=get_to_int(get_request('rec_id')); //receiver id
			$message_draft=get_to_int(get_request('draft_id')); //draft id
			if($message_draft) {
				$row=mysql_fetch_row(oj_query('select receiver_id,title,content from message ' . 
					"where id=$message_draft and sender_id=$_user_id and state='draft' limit 1 "));
				if(!isset($row[0]))redirect_error('Draft not found.');
				$message_rec     = $row[0];
				$message_title   = $row[1];
				$message_content = $row[2];
			}
			$message_rec_name=user_id_to_name($message_rec);
		}
		if($message_rec==0||!isset($message_rec_name))redirect_error('Need receiver id.');
		$message_rec_name=str_to_html($message_rec_name);
		
		include_once 'include/form.php';
		draw_simple_form('post','message.php',array('action'=>'send','rec_id'=>$message_rec,'draft_id'=>$message_draft),array(
			'receiver'=>array('html','Receiver:',"<a href='user.php?user_id=$message_rec'>$message_rec_name($message_rec)</a>"),
			'title'=>array('input','Title:',$message_title,' maxlength="200" '),
			'content'=>array('textarea','Content:',$message_content),
			'submit'=>array('submit','','Send')
		));
	} else if($message_action=='read'||$message_action=='review') {
		$message_id=get_to_int(get_request('id'));
		$message_raw=get_request('raw');
		$query='select sender_id,receiver_id,title,content,send_time from message ';
		if($message_action=='read')
			$query.="where id=$message_id and receiver_id=$_user_id and state!='draft' limit 1 ";
		else
			$query.="where id=$message_id and sender_id=$_user_id and state!='draft' limit 1 ";
		$row=mysql_fetch_row(oj_query($query));
		if(!isset($row[0]))redirect_error('Message not found.');
		$message_sender  = $row[0];
		$message_rec     = $row[1];
		$message_title   = str_to_html($row[2]);
		$message_content = $row[3];
		$message_time    = $row[4];
		$message_sender_name = str_to_html(user_id_to_name($message_sender));
		$message_rec_name    = str_to_html(user_id_to_name($message_rec));
		if(empty($message_raw))$message_content=str_replace("\n","<br>\n",str_to_html($message_content));
		echo '<div class="problem_content">';
			echo "Sender:<a href='user.php?user_id=$message_sender'>$message_sender_name</a><br/>";
			echo "Receiver:<a href='user.php?user_id=$message_rec'>$message_rec_name</a><br/>";
			echo "Time:$message_time<br/>";
			echo "Title:$message_title<br/>";
			echo '<hr/>';
			echo "<div id='main_content'>$message_content</div>";
		echo '</div>';
		echo '<center>';
			if(empty($message_raw))echo "[<a href='message.php?action=$message_action&id=$message_id&raw=1'>View as html</a>]&nbsp;";
			else echo "[<a href='message.php?action=$message_action&id=$message_id'>View as plain text</a>]&nbsp;";
			if($message_action=='read')echo "[<a href='message.php?action=reply&rep_id=$message_id'>Reply</a>]&nbsp;";
		echo '</center>';
		
		if($message_action=='read')
			oj_query("update message set state='read' where id=$message_id ");
	} else { //in/out
		$message_limit=20;
		$message_top=get_to_int(get_request('top'));
		if($message_top)$message_top="and id<$message_top";
		else $message_top='';
		if($message_action=='out') {
			$query='select id,receiver_id,title,send_time,state from message where ' . 
				" sender_id=$_user_id $message_top order by id desc limit $message_limit ";
			$message_read='review';
			draw_tablelist_head(array('Receiver',array('Title','width="70%"'),'Time'));
		} else { //in
			$query='select id,sender_id,title,send_time,state from message where ' . 
				" receiver_id=$_user_id and state!='draft' $message_top order by id desc limit $message_limit ";
			$message_read='read';
			draw_tablelist_head(array('Sender',array('Title','width="70%"'),'Time'));
		}
		$ret=oj_query($query);
		$count=0;
		while($row=mysql_fetch_row($ret)) {
			$now_id     = $row[0];
			$now_user   = $row[1];
			$now_title  = str_to_html($row[2]);
			$now_time   = $row[3];
			$now_state  = $row[4];
			$now_user_name = str_to_html(user_id_to_name($now_user));
			if(empty($now_title))$now_title='(No title)';
			if($now_state=='sent')echo '<tr style="font-weight:bold;">';else echo '<tr>';
				echo "<td><a href='user.php?user_id=$now_user'>$now_user_name</a></td>";
				echo "<td><a href='message.php?action=$message_read&id=$now_id'>$now_title</a></td>";
				echo "<td>$now_time</td>";
			echo '</tr>';
			$count++;
		}
		echo '</table>';
		if($count==$message_limit)
			echo "<center>[<a href='message.php?action=$message_action&top=$now_id'>Next page</a>]</center>";
	}

	include 'include/footer.php';
?>

